﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 影楼婚纱摄影系统
{
    public partial class 套系销量 : Form
    {
        public 套系销量()
        {
            InitializeComponent();
        }

        private void 套系销量_Load(object sender, EventArgs e)
        {
            QueryAll();
        }
        private void QueryAll()
        {
            //数据库连接串
            //string connStr = "Data Source=DESKTOP-P8IR1OF;Initial Catalog=marry;Integrated Security=True";
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["marry"].ConnectionString;
            //创建SqlConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(str);
                //打开数据库
                conn.Open();
                string sql = "Select sets.setsid as 套系ID,sets.setsname as 套系名,COUNT([order].setsId) as 订单数量 From sets INNER JOIN [order] ON sets.setsid=[order].setsId GROUP BY sets.setsid,setsname ORDER BY COUNT([order].setsId) DESC";
                //创建SqlDataAdapter类的对象
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                //创建DataSet类的对象
                DataSet ds = new DataSet();
                //使用SqlDataAdapter对象sda将查新结果填充到DataSet对象ds中
                sda.Fill(ds);
                //设置表格控件的DataSource属性
                dataGridView1.DataSource = ds.Tables[0];
                //设置数据表格上显示的列标题
                dataGridView1.Columns[0].HeaderText = "套系ID";
                dataGridView1.Columns[1].HeaderText = "套系名";
                dataGridView1.Columns[2].HeaderText = "订单数量";
                //设置数据表格为只读
                dataGridView1.ReadOnly = true;
                //不允许添加行
                dataGridView1.AllowUserToAddRows = false;
                //背景为白色
                dataGridView1.BackgroundColor = Color.White;
                //只允许选中单行
                dataGridView1.MultiSelect = false;
                //整行选中
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询错误！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }
    }
}
